﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using CallBox.Entity;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;

namespace CallBox.Dao
{
    public class clsMobileDAO
    {
           private static clsMobileDAO objIntancia;
        private static Mutex objMutex = new Mutex();
        public static clsMobileDAO Intancia
        {
            get
            {
                objMutex.WaitOne();
                if (objIntancia == null)
                {
                    objIntancia = new clsMobileDAO();
                }
                objMutex.ReleaseMutex();
                return objIntancia;
            }
        }
        public clsMobileDAO()
        {
        }

        private clsMobile GetEntity(DbDataReader Reader)
       {
           clsMobile objMessage = new clsMobile();
           objMessage.Id = Reader["Id"] is DBNull ? 0 : Convert.ToInt32(Reader["Id"]);
           objMessage.Number = Reader["Number"] is DBNull ? "" : Convert.ToString(Reader["Number"]);
           objMessage.Password = Reader["Password"] is DBNull ? "" : Convert.ToString(Reader["Password"]);
           objMessage.AuthCode = Reader["AuthCode"] is DBNull ? "" : Convert.ToString(Reader["AuthCode"]);
           objMessage.TypeAccount = Reader["TypeAccount"] is DBNull ? "" : Convert.ToString(Reader["TypeAccount"]);
           objMessage.IdWelcomeMessage = Reader["IdWelcomeMessage"] is DBNull ? 0 : Convert.ToInt32(Reader["IdWelcomeMessage"]);
           objMessage.IdCountry = Reader["IdCountry"] is DBNull ? 0 : Convert.ToInt32(Reader["IdCountry"]);
           objMessage.Email = Reader["Email"] is DBNull ? "" : Convert.ToString(Reader["Email"]);
           objMessage.IsLinked = Reader["IsLinked"] is DBNull ? 0 : Convert.ToInt32(Reader["IsLinked"]);
           objMessage.Status = Reader["Status"] is DBNull ? 0 : Convert.ToInt32(Reader["Status"]);
           return objMessage;
       }

        public clsMobile Validate(String Number, String Password)
       {
           SqlConnection db = null;
           clsMobile Objeto = null;
           DbDataReader Reader = null;
           try
           {
               db = clsConexion.ObtenerConecccion();
               SqlCommand ObjCmd = new SqlCommand("SP_Mobile_Validate", db);
               ObjCmd.Parameters.AddWithValue("@Number", Number);
               ObjCmd.Parameters.AddWithValue("@Password", Password);

               ObjCmd.CommandType = CommandType.StoredProcedure;
               Reader = ObjCmd.ExecuteReader();

               if (Reader.HasRows)
               {
                   if (Reader.Read())
                   {
                       Objeto = GetEntity(Reader);
                      
                   }
               }
               Reader.Close();
           }
           catch (Exception)
           {
               throw;
           }
           return Objeto;
       }

        public clsMobile GetAuthCode(String Number)
        {
            SqlConnection db = null;
            clsMobile Objeto = null;
            DbDataReader Reader = null;
            try
            {
                db = clsConexion.ObtenerConecccion();
                SqlCommand ObjCmd = new SqlCommand("SP_GET_AUTHCODE", db);
                ObjCmd.Parameters.AddWithValue("@NUMBER", Number);

                ObjCmd.CommandType = CommandType.StoredProcedure;
                Reader = ObjCmd.ExecuteReader();

                if (Reader.HasRows)
                {
                    if (Reader.Read())
                    {
                        Objeto = new clsMobile();
                        Objeto.AuthCode = Reader["AuthCode"] is DBNull ? "" : Convert.ToString(Reader["AuthCode"]);
                    }
                }
                
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                Reader.Close();
                db.Close();                
            }
            return Objeto;
        }

        public String ValidateFirstAccess(String Number, String AuthCode)
        {
            SqlConnection db = null;            
            Int32 count = 0;
            String result = "";
            try
            {
                db = clsConexion.ObtenerConecccion();
                SqlCommand ObjCmd = new SqlCommand("SP_VALIDATE_FIRSTACCESS", db);
                ObjCmd.Parameters.AddWithValue("@NUMBER", Number);
                ObjCmd.Parameters.AddWithValue("@AUTHCODE", AuthCode);

                ObjCmd.CommandType = CommandType.StoredProcedure;
                count = Convert.ToInt32(ObjCmd.ExecuteScalar());

                if (count == 1)
                    result = "success";
                else
                    result = "Error validating mobile";
            }
            catch (Exception)
            {
                result = "Error validating mobile";
                throw;
            }
            finally
            {              
                db.Close();
            }
            return result;
        }
    }
}
